package by.vkhalko.asamo.services.impl;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.vkhalko.asamo.dataaccess.ContactDao;
import by.vkhalko.asamo.datamodel.Contact;
import by.vkhalko.asamo.datamodel.Person;
import by.vkhalko.asamo.services.ContactService;

@Service
public class ContactServiceImpl implements ContactService {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(ContactServiceImpl.class);

	@Inject
	private ContactDao dao;

	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of ContactService is created. Class is: {}",
				getClass().getName());
	}

	@Override
	public Contact get(Long id) {
		Contact entity = dao.getById(id);
		return entity;
	}

	@Override
	public void saveOrUpdate(Contact contact) {
		if (contact.getId() == null) {
			LOGGER.debug("Save new: {}", contact);
			dao.insert(contact);
		} else {
			LOGGER.debug("Update: {}", contact);
			dao.update(contact);
		}
	}

	@Override
	public void delete(Contact contact) {
		LOGGER.debug("Remove: {}", contact);
		dao.delete(contact.getId());

	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all contacts");
		dao.deleteAll();
	}

	@Override
	public List<Contact> getAllContacts() {
		return dao.getAllContacts();

	}

	@Override
	public List<Contact> getAllByPerson(Person person) {

		return dao.getAllByPerson(person);
	}

}
